//
//  NewsViewController.m
//  结构
//
//  Created by 看着太阳吃烧饼 on 15/7/17.
//  Copyright (c) 2015年 看着太阳吃烧饼. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsModal.h"
#import "NewsCellT.h"
#import "DataService.h"
#import "UIViewExt.h"
#import "ViewController.h"

#define kwidth [UIScreen mainScreen].bounds.size.width
#define kheight [UIScreen mainScreen].bounds.size.height
@interface NewsViewController ()
{
    UIImageView *_headerImageView;
    UILabel *_headerLabel;
}
@end

@implementation NewsViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"新闻";
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _createNewsTableView];
    [self _creatHeaderView];
    [self _loadData];
//    self.view.backgroundColor = [UIColor redColor];
    // Do any additional setup after loading the view.
}
#pragma mark - create

- (void)_creatHeaderView {
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 200)];
    headerView.backgroundColor = [UIColor clearColor];
    [_newsTableView setTableHeaderView:headerView];
    
    _headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 64, kwidth, 200)];
//    _headerImageView.image = [UIImage imageNamed:@"19@2x.png"];
    _headerImageView.contentMode = UIViewContentModeScaleAspectFit;
    [self.view addSubview:_headerImageView];
    
    _headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kwidth, 30)];
    _headerLabel.bottom = _headerImageView.bottom;
    _headerLabel.textColor = [UIColor whiteColor];
    
    _headerLabel.backgroundColor = [UIColor grayColor];
    [self.view addSubview:_headerLabel];
    
}
- (void)_createNewsTableView {
    _newsTableView = [[UITableView alloc] initWithFrame:self.view.bounds];
//    _newsTableView.backgroundColor = [UIColor blackColor];
    _newsTableView.delegate = self;
    _newsTableView.dataSource = self;
    
    [self.view addSubview:_newsTableView];
    //注册
    UINib *nib = [UINib nibWithNibName:@"NewsCellT" bundle:[NSBundle mainBundle]];
    [_newsTableView registerNib:nib forCellReuseIdentifier:@"cell"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _newsModalArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NewsCellT *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    NewsModal *modal = _newsModalArray[indexPath.row];
    cell.backgroundColor = [UIColor blackColor];
    cell.modal = modal;
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 80;
}

#pragma mark - loadData
- (void)_loadData {
    _newsModalArray = [[NSMutableArray alloc] init];
    //第一种方法
//    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"news_list.json" ofType:nil];
//    NSData *data = [NSData dataWithContentsOfFile:filePath];
//    NSArray *dataArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
//    for (int i = 0; i<dataArray.count; i++) {
//        NewsModal *modal = [[NewsModal alloc] init];
//        modal.title = [dataArray[i] objectForKey:@"title"];
//        modal.summary = [dataArray[i] objectForKey:@"summary"];
//        modal.image = [dataArray[i] objectForKey:@"image"];
//        NSNumber *typeNum = [dataArray[i] objectForKey:@"type"];
//        modal.type = [typeNum integerValue];
//        [_newsModalArray addObject:modal];
//    }
    //第二种方法
    NSArray *newsArray = [DataService getJsonDataFromFile:@"news_list.json"];
    for (NSDictionary *dic in newsArray) {
        NewsModal *modal = [[NewsModal alloc] init];
        [modal setValuesForKeysWithDictionary:dic];
        [_newsModalArray addObject:modal];
    }
    NewsModal *modal = _newsModalArray[0];
    _headerLabel.text = modal.title;
    NSURL *url = [NSURL URLWithString:modal.image];
    [_headerImageView sd_setImageWithURL:url];
    [_newsModalArray removeObjectAtIndex:0];
//    modal.image = []
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat offY = scrollView.contentOffset.y;
    if (offY > -64) {
        _headerImageView.top = -offY;
        _headerLabel.bottom = _headerImageView.bottom;
    }
    else {
        CGFloat newHeight = ABS(offY)-64+200;
        CGFloat newWidth = kheight/200*kwidth;
        _headerImageView.frame = CGRectMake((kwidth-newWidth)/2, 64, newWidth, newHeight);
        _headerLabel.bottom = _headerImageView.bottom;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ViewController *vc = [[ViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
